﻿#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author: Even Chow
@software: PyCharm
@time: 2016/12/16 13:35
"""

import time
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import requests

# 直接登陆新浪微博
url = 'http://weibo.com/login.php'
binary = FirefoxBinary('E:\\basic softer\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary,executable_path='C:/Users/Administrator/Desktop/spyder/geckodriver.exe')
driver.get(url)
print('开始登陆')

# 定位到账号密码表单
login_tpye = driver.find_element_by_class_name('info_header').find_element_by_xpath('//a[2]')
login_tpye.click()
time.sleep(3)

name_field = driver.find_element_by_id('loginname')
name_field.clear()
name_field.send_keys('your account')

password_field = driver.find_element_by_class_name('password').find_element_by_name('password')
password_field.clear()
password_field.send_keys('your password')


submit = driver.find_element_by_class_name('W_login_form').find_element_by_link_text('登录')
submit.click()

# 等待页面刷新，完成登陆
time.sleep(5)
print('登陆完成')
sina_cookies = driver.get_cookies()

cookie = [item["name"] + "=" + item["value"] for item in sina_cookies]
cookiestr = '; '.join(item for item in cookie)

# 进入投票页面刷票
i = 0;
while(i<20):
    driver.get('http://huodong.weibo.com/netchina2016?ref=feed&from=501?from=501')
    ele_vote = driver.find_element_by_xpath('//a[@href="http://weibo.com/u/1259110474"]//..//..//dl//dd[2]//span[2]//a')
    ele_vote.click()
    i = i+1
    time.sleep(5)
# html = driver.page_source
# print html
# ele_continue = driver.find_element_by_xpath('//div[@id="layer_14818662097129"]//div//div[2]//p2//a')
# while(ele_continue):
#     ele_continue.click()


"""
redirect_url = 'http://huodong.weibo.com/netchina2016?ref=feed&from=501?from=501'
headers = {'cookie': cookiestr}
html = requests.get(redirect_url, headers=headers).text
print(html)
"""
